/**
 * 
 */
package com.worksmap.core.util;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
import com.sdicons.json.model.JSONValue;

/**
 * @author jack
 *
 */
public class JsonUtil {

	/**
	 * 方法简介：转换json数据为list
	 * 数据格式："[{'payTime':'2013-12-15 22:10:49','totalFee':'1000.00'},{'payTime':'2013-12-15 21:53:17','totalFee':'1000.00'}]"
	 * bean中的日期数据定义为String，若是date则会出错
	 * jack
	 * 2013-12-18
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> parseJsonToList(String json, Class<T> beanClass){
		List<T> beanList = new ArrayList<T>();
		JSONArray jsonarray = JSONArray.fromObject(json);  
		beanList = (List<T>)JSONArray.toCollection(jsonarray, beanClass);
		return beanList;
	}
	
	
	/** 
     * JAVA对象转换成JSON字符串 
     * @param obj 
     * @return 
     * @throws MapperException 
     */   
    public static String objectToJsonStr(Object obj) throws MapperException{  
        JSONValue jsonValue = JSONMapper.toJSON(obj);
        String jsonStr = jsonValue.render(false);  
        return jsonStr;  
    }  
	
	
}
